package com.woniu.tms.manage.service.impl;

import com.woniu.tms.entity.Role;
import com.woniu.tms.entity.RoleExample;
import com.woniu.tms.entity.UserRole;
import com.woniu.tms.entity.UserRoleExample;
import com.woniu.tms.manage.service.RoleService;
import com.woniu.tms.mapper.RoleMapper;
import com.woniu.tms.mapper.UserRoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
@Transactional
public class RoleServiceImpl implements RoleService {

    @Autowired
    RoleMapper roleMapper;
    @Autowired
    UserRoleMapper userRoleMapper;

    @Override
    public List<Role> unAssignRoleList(Integer uid) {
        // 得到该用户的角色关系列表
        UserRoleExample example1 = new UserRoleExample();
        UserRoleExample.Criteria criteria1 = example1.createCriteria();
        criteria1.andUidEqualTo(uid);
        List<UserRole> userRoleList = userRoleMapper.selectByExample(example1);
        List<Integer> list = new ArrayList<>();
        // 得到该用户所有的角色id
        for (UserRole userRole : userRoleList) {
            list.add(userRole.getRid());
        }
        // 得到该用户未分配的角色
        RoleExample example2 = new RoleExample();
        if (list.size() > 0) {
            RoleExample.Criteria criteria2 = example2.createCriteria();
            criteria2.andRidNotIn(list);
        }
        return roleMapper.selectByExample(example2);
    }

    @Override
    public List<Role> assignRoleList(Integer uid) {
        // 得到该用户的角色关系列表
        UserRoleExample example1 = new UserRoleExample();
        UserRoleExample.Criteria criteria1 = example1.createCriteria();
        criteria1.andUidEqualTo(uid);
        List<UserRole> userRoleList = userRoleMapper.selectByExample(example1);
        List<Integer> list = new ArrayList<>();
        // 得到该用户所有的角色id
        for (UserRole userRole : userRoleList) {
            list.add(userRole.getRid());
        }
        // 得到该用户已分配的角色
        List<Role> roleList = new ArrayList<>();
        RoleExample example2 = new RoleExample();
        if (list.size() > 0) {
            RoleExample.Criteria criteria2 = example2.createCriteria();
            criteria2.andRidIn(list);
            roleList = roleMapper.selectByExample(example2);
        }
        return roleList;
    }

    @Override
    public List<Role> getByCondition(String condition) {
        RoleExample example = new RoleExample();
        RoleExample.Criteria criteria = example.createCriteria();
        criteria.andRnameLike("%" + condition + "%");
        return roleMapper.selectByExample(example);
    }

    @Override
    public void delete(Integer rid) {
        RoleExample example = new RoleExample();
        RoleExample.Criteria criteria = example.createCriteria();
        criteria.andRidEqualTo(rid);
        roleMapper.deleteByExample(example);
    }

    @Override
    public void add(Role role) {
        roleMapper.insert(role);
    }

    @Override
    public void update(Role role) {
        roleMapper.updateByPrimaryKey(role);
    }

    @Override
    public Role selectByRid(Integer rid) {
        return roleMapper.selectByPrimaryKey(rid);
    }

    @Override
    public void delAll(Integer[] rids) {
        RoleExample example = new RoleExample();
        RoleExample.Criteria criteria = example.createCriteria();
        criteria.andRidIn(Arrays.asList(rids));
        roleMapper.deleteByExample(example);
    }

}
